<?php
   
$str_delimiter = ';';
$str_eol       = "\n";
$str_enclosure = '"';

$int_headers = 0;
$arr_headers = $this->arr_headers;
$arr_columns = array_pop($arr_headers);
$int_columns = count($arr_columns);

// use 'caption' if possible
$str_caption = isset($this->arr_properties['caption']) ? $this->arr_properties['caption'] : null;

$arr_sorting = isset($this->arr_properties['sorting']) ? $this->arr_properties['sorting'] : array();

$arr_groups = isset($this->arr_data['groups']) ? $this->arr_data['groups'] : array();

if(!empty($str_caption)) {
    
    $arr_csv = array($str_caption);
    for($i = 0; $i < ($int_columns-1); $i++) {
        $arr_csv[] = '';
    }
    $arr_csv = array($arr_csv);
    
    echo Utility_CsvFunctions::generateCsvAsString($arr_csv, $str_delimiter, $str_eol, $str_enclosure);
}

$arr_csv_headers = array();

//==============================================================================
// Headers
//==============================================================================

foreach($arr_headers as $arr_header_row) {
    
    $arr_temp = array();
    foreach($arr_header_row as $str_key => $mix_header) {
        
        $arr_header = (array) $mix_header;
        $str_label = $str_key;
        
        if(is_string($mix_header)) {
            $str_label = $mix_header;
        } else if(isset($arr_header['label'])) {
            $str_label = $arr_header['label'];
        }
        
        $int_children = isset($arr_header['children']) ? (int) $arr_header['children'] : 1;
        
        $arr_temp[] = $str_label;
        
        for($i = 1; $i < $int_children; $i++) {
            $arr_temp[] = '';
        }
    }
    
    if(!empty($arr_temp)) {
        $arr_csv_headers[] = $arr_temp;
    }
    
    $int_headers++;
}

//==============================================================================
// Columns
//==============================================================================

$arr_temp = array();
foreach($arr_columns as $str_key => $mix_header) {
    
    $arr_header = (array) $mix_header;
    $str_label = $str_key;
    
    if(is_string($mix_header)) {
        $str_label = $mix_header;
    } else if(isset($arr_header['label'])) {
        $str_label = $arr_header['label'];
    }

    $arr_temp[] = $str_label;
    
}

if(!empty($arr_temp)) {
    $arr_csv_headers[] = $arr_temp;
}

$int_headers++;

echo Utility_CsvFunctions::generateCsvAsString($arr_csv_headers, $str_delimiter, $str_eol, $str_enclosure);

//==============================================================================
// Body
//==============================================================================

$arr_csv_data = array();

$arr_rows = $this->arr_data['rows'];
foreach($arr_rows as $str_group_key => $arr_group_rows) {

    $arr_group_meta = isset($arr_groups[$str_group_key])
        ? $arr_groups[$str_group_key]
        : array();
    
    $str_group_label = isset($arr_group_meta['label']) ? $arr_group_meta['label'] : null;
    $str_group_depth = isset($arr_group_meta['depth']) ? $arr_group_meta['depth'] : 1;

    if(!is_null($str_group_label)) {
        $arr_csv_temp = array($str_group_label);
        for($i = 0; $i < ($int_columns-1); $i++) {
            $arr_csv_temp[] = '';
        }
        $arr_csv_data[] = $arr_csv_temp;
    }

    if(isset($arr_group_meta['headers'])) {
        
        $i = 0;
        foreach($arr_group_meta['headers'] as $str_row_key => $arr_row) {
            $arr_csv_temp = Common_DataTable_View_Csv::generateRow($i, $arr_row, $arr_columns);
            $arr_csv_data[] = $arr_csv_temp;
            $i++;
        }
    
    }
    
    if(is_array($arr_group_rows) || ($arr_group_rows instanceof IteratorAggregate)) {

        $i = 0;
        foreach($arr_group_rows as $str_row_key => $arr_row) {
            $arr_csv_temp = Common_DataTable_View_Csv::generateRow($i, $arr_row, $arr_columns);
            $arr_csv_data[] = $arr_csv_temp;      
            $i++;
        }

    }
    
    if(isset($arr_group_meta['footers'])) {
        
        $i = 0;
        foreach($arr_group_meta['footers'] as $str_row_key => $arr_row) {
            $arr_csv_temp = Common_DataTable_View_Csv::generateRow($i, $arr_row, $arr_columns);
            $arr_csv_data[] = $arr_csv_temp;
            $i++;
        }
    
    }

}

echo Utility_CsvFunctions::generateCsvAsString($arr_csv_data, $str_delimiter, $str_eol, $str_enclosure);

//==============================================================================
// Footers
//==============================================================================

$arr_csv_footers = array();

if(isset($this->arr_footers) && !empty($this->arr_footers)) {

    $i = 0;
    foreach($this->arr_footers as $arr_row) {
        
        $int_column_index = 0;
        
        $arr_temp = array();
        foreach($arr_columns as $str_key => $mix_header) {
            
            $arr_header = is_string($mix_header) ? array('label' => $mix_header) : (array) $mix_header;
            
            $str_source_key = isset($arr_header['source']) ? $arr_header['source'] : $str_key;
            
            $mix_value = isset($arr_row[$str_source_key]) ? $arr_row[$str_source_key] : null;
            
            $arr_temp[] = $mix_value;
            
            $int_column_index++;
        }
        
        $arr_csv_footers[] = $arr_temp;
    
        $i++;
        
    }
    
    

}

if(!empty($arr_csv_footers)) {
    echo Utility_CsvFunctions::generateCsvAsString($arr_csv_footers, $str_delimiter, $str_eol, $str_enclosure);
}


